using System;
using System.Windows.Forms;
using DataModel.Reports;
using DevExpress.XtraEditors;
using DataModel;
using DevExpress.XtraEditors.Controls;

namespace RitaReports.WorkflowResponse
{
    public partial class WorkflowResponseFilter : XtraUserControl, IReportFilter
    {
        public event EventHandler FilterConditiondsChanged = delegate { };

        private bool inited;
         
        public bool Inited
        {
            get { return inited; }
        }

        public WorkflowResponseFilter()
        {
            InitializeComponent();
        }

        #region IReportFilter Members

        public Control FilterControl
        {
            get { return this; }
        }

        public IReportFilterConditionds FilterConditionds
        {
            get
            {
                WorkflowResponseFilterConditionds conditionds = new WorkflowResponseFilterConditionds();

                if (cmbOrganization.Properties.Items.Count > 0 &&
                    cmbOrganization.SelectedIndex != 0)
                {
                    conditionds.OrgId = (int) cmbOrganization.Properties.Items[cmbOrganization.SelectedIndex].Value;
                    conditionds.OrgName = cmbOrganization.Properties.Items[cmbOrganization.SelectedIndex].Description;
                }

                if (cbSort.Properties.Items.Count > 0 && cbSort.SelectedIndex >= 0)
                {
                    conditionds.SortOrder = (ApproveReportSortOrder) cbSort.Properties.Items[cbSort.SelectedIndex].Value;
                }

                return conditionds;
            }
        }

        #endregion


        public void Init(WorkflowResponseReportInitData initData)
        {
            cmbOrganization.Properties.Items.Clear();
            cmbOrganization.Properties.Items.Add(new ImageComboBoxItem("All", 0, 0));
            foreach (Organization org in initData.Units)
            {
                cmbOrganization.Properties.Items.Add(new ImageComboBoxItem(org.Name, org.Id, 1));
            }
            cmbOrganization.SelectedIndex = 0;

            cbSort.Properties.Items.Clear();
            cbSort.Properties.Items.Add(new ImageComboBoxItem("Slowest to Fastest", ApproveReportSortOrder.SlowestToFastest, 2));
            cbSort.Properties.Items.Add(new ImageComboBoxItem("Fastest to Slowest", ApproveReportSortOrder.FastestToSlowest, 3));
            cbSort.SelectedIndex = 0;

            inited = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cmbOrganization.SelectedIndex = 0;
        }

        private void cmbOrganization_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbSort.Enabled = (cmbOrganization.SelectedIndex == 0);

            if (inited)
            {
                this.FilterConditiondsChanged(this, EventArgs.Empty);
            }
        }

        private void cbSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (inited)
            {
                this.FilterConditiondsChanged(this, EventArgs.Empty);
            }
        }
    }
}